Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Dialog] Improve scroll=body CSS logic #15896

Merged
merged 4 commits into from
May 31, 2019
Merged

[Dialog] Improve scroll=body CSS logic #15896

merged 4 commits into from
May 31, 2019

Conversation

DominikSerafin
Copy link
Contributor

@DominikSerafin DominikSerafin commented May 27, 2019


This PR tackles two things:

  1. Dialog scroll=body is now always vertically centered instead on top of the page / Dialog with scroll=body is not centered #12130

  2. I found out while working on the centering issue that fullWidth=false was ignored with scroll=body. The content was always in full width (just like with fullWidth=true). This should be now fixed.


I've tested this manually with various props (maxWidth, fullWidth, fullScreen, etc.) on various resolutions and browsers (Windows 10 Chrome, Firefox, Edge, IE 11) and seems to be working OK.


This is my second PR to material-ui and I'm not too familiar with its source so please let me know if I did something improperly and I'll get on fixing that!

@mui-pr-bot
Copy link

mui-pr-bot commented May 27, 2019

Details of bundle changes.

Comparing: 831c6cc...5da996d

bundle parsed diff gzip diff prev parsed current parsed prev gzip current gzip
@material-ui/core +0.07% 🔺 +0.01% 🔺 315,057 315,293 86,304 86,315
@material-ui/core/Paper 0.00% 0.00% 67,921 67,921 20,184 20,184
@material-ui/core/Paper.esm 0.00% 0.00% 61,217 61,217 18,981 18,981
@material-ui/core/Popper 0.00% 0.00% 28,740 28,740 10,345 10,345
@material-ui/core/Textarea 0.00% 0.00% 5,513 5,513 2,376 2,376
@material-ui/core/TrapFocus 0.00% 0.00% 3,744 3,744 1,573 1,573
@material-ui/core/styles/createMuiTheme 0.00% 0.00% 15,978 15,978 5,787 5,787
@material-ui/core/useMediaQuery 0.00% 0.00% 2,106 2,106 975 975
@material-ui/lab 0.00% 0.00% 138,866 138,866 42,660 42,660
@material-ui/styles 0.00% 0.00% 51,386 51,386 15,193 15,193
@material-ui/system 0.00% 0.00% 14,463 14,463 4,181 4,181
Button 0.00% 0.00% 83,901 83,901 25,459 25,459
Modal 0.00% 0.00% 20,343 20,343 6,685 6,685
colorManipulator 0.00% 0.00% 3,904 3,904 1,543 1,543
docs.landing 0.00% 0.00% 55,977 55,977 14,042 14,042
docs.main 0.00% 0.00% 648,947 648,979 204,664 204,672
packages/material-ui/build/umd/material-ui.production.min.js +0.08% 🔺 -0.01% 293,979 294,215 83,766 83,759

Generated by 🚫 dangerJS against 5da996d

@oliviertassinari oliviertassinari added the component: dialog This is the name of the generic UI component, not the React module! label May 27, 2019
@oliviertassinari
Copy link
Member

oliviertassinari commented May 27, 2019

@DominikSerafin Thank you for sharing this! If you don't mind, I would like to explore the simplifications potential leveraging your vertical-align method. Can you spot any problem with:

diff --git a/packages/material-ui/src/Dialog/Dialog.js b/packages/material-ui/src/Dialog/Dialog.js
index 6ddadabaf..bab4ed461 100644
--- a/packages/material-ui/src/Dialog/Dialog.js
+++ b/packages/material-ui/src/Dialog/Dialog.js
@@ -29,7 +29,6 @@ export const styles = theme => ({
   scrollBody: {
     overflowY: 'auto',
     overflowX: 'hidden',
-    width: '100%',
     textAlign: 'center',
     '&:after': {
       content: '""',
@@ -50,8 +49,6 @@ export const styles = theme => ({
   },
   /* Styles applied to the `Paper` component. */
   paper: {
-    display: 'flex',
-    flexDirection: 'column',
     margin: 48,
     position: 'relative',
     overflowY: 'auto', // Fix IE 11 issue, to remove at some point.
@@ -62,81 +59,44 @@ export const styles = theme => ({
   },
   /* Styles applied to the `Paper` component if `scroll="paper"`. */
   paperScrollPaper: {
+    display: 'flex',
-    flex: '0 1 auto',
+    flexDirection: 'column',
     maxHeight: 'calc(100% - 96px)',
   },
   /* Styles applied to the `Paper` component if `scroll="body"`. */
   paperScrollBody: {
-    margin: '48px auto',
     display: 'inline-block',
     verticalAlign: 'middle',
     textAlign: 'left', // 'initial' doesn't work on IE 11
   },
   /* Styles applied to the `Paper` component if `maxWidth=false`. */
   paperWidthFalse: {
-    '&$paperScrollBody': {
-      margin: 48,
-      maxWidth: 'calc(100% - 96px)',
-    },
+    maxWidth: 'calc(100% - 96px)',
   },
   /* Styles applied to the `Paper` component if `maxWidth="xs"`. */
   paperWidthXs: {
     maxWidth: Math.max(theme.breakpoints.values.xs, 444),
-    '&$paperScrollBody': {
-      maxWidth: `calc(${Math.max(theme.breakpoints.values.xs, 444)}px - 96px)`,
-      [theme.breakpoints.down(Math.max(theme.breakpoints.values.xs, 444) + 48 * 2)]: {
-        margin: 48,
-        maxWidth: 'calc(100% - 96px)',
-      },
-    },
   },
   /* Styles applied to the `Paper` component if `maxWidth="sm"`. */
   paperWidthSm: {
     maxWidth: theme.breakpoints.values.sm,
-    '&$paperScrollBody': {
-      maxWidth: `calc(${theme.breakpoints.values.sm}px - 96px)`,
-      [theme.breakpoints.down(theme.breakpoints.values.sm + 48 * 2)]: {
-        margin: 48,
-        maxWidth: 'calc(100% - 96px)',
-      },
-    },
   },
   /* Styles applied to the `Paper` component if `maxWidth="md"`. */
   paperWidthMd: {
     maxWidth: theme.breakpoints.values.md,
-    '&$paperScrollBody': {
-      maxWidth: `calc(${theme.breakpoints.values.md}px - 96px)`,
-      [theme.breakpoints.down(theme.breakpoints.values.md + 48 * 2)]: {
-        margin: 48,
-        maxWidth: 'calc(100% - 96px)',
-      },
-    },
   },
   /* Styles applied to the `Paper` component if `maxWidth="lg"`. */
   paperWidthLg: {
     maxWidth: theme.breakpoints.values.lg,
-    '&$paperScrollBody': {
-      maxWidth: `calc(${theme.breakpoints.values.lg}px - 96px)`,
-      [theme.breakpoints.down(theme.breakpoints.values.lg + 48 * 2)]: {
-        margin: 48,
-        maxWidth: 'calc(100% - 96px)',
-      },
-    },
   },
   /* Styles applied to the `Paper` component if `maxWidth="xl"`. */
   paperWidthXl: {
     maxWidth: theme.breakpoints.values.xl,
-    '&$paperScrollBody': {
-      maxWidth: `calc(${theme.breakpoints.values.xl}px - 96px)`,
-      [theme.breakpoints.down(theme.breakpoints.values.xl + 48 * 2)]: {
-        margin: 48,
-        maxWidth: 'calc(100% - 96px)',
-      },
-    },
   },
   /* Styles applied to the `Paper` component if `fullWidth={true}`. */
   paperFullWidth: {
-    width: '100%',
+    width: 'calc(100% - 96px)',
   },
   /* Styles applied to the `Paper` component if `fullScreen={true}`. */
   paperFullScreen: {
@@ -148,8 +108,6 @@ export const styles = theme => ({
     borderRadius: 0,
     '&$paperScrollBody': {
       margin: 0,
-      width: '100%',
-      maxWidth: '100%',
     },
   },
 });

Let us know, thanks!

@oliviertassinari oliviertassinari changed the title [Dialog][scroll=body] Vertically center paper and fix fullWidth=false [Dialog] Improve scroll=body CSS logic May 27, 2019
@DominikSerafin
Copy link
Contributor Author

DominikSerafin commented May 28, 2019

Hey @oliviertassinari, this seems to have a problem where the dialog is not properly responsive:

  1. Add <textarea> element in the dialog content
  2. Set dialog to maxWidth larger than viewport
  3. Set dialog to fullWidth=false
  4. Resize the textarea horizontally to be larger than dialog maxWidth
  5. The dialog now goes outside the viewport and gets shifted to top
    • basically - elements scrollBody:after and paperScrollBody with display: inline-block get stacked on each other instead beside each other

image

@oliviertassinari
Copy link
Member

@DominikSerafin Well spotted! Alright, Let's rollback a bit from my too aggressive proposal.
What about the following?

diff --git a/packages/material-ui/src/Dialog/Dialog.js b/packages/material-ui/src/Dialog/Dialog.js
index 6ddadabaf..99b26f214 100644
--- a/packages/material-ui/src/Dialog/Dialog.js
+++ b/packages/material-ui/src/Dialog/Dialog.js
@@ -29,7 +29,6 @@ export const styles = theme => ({
   scrollBody: {
     overflowY: 'auto',
     overflowX: 'hidden',
-    width: '100%',
     textAlign: 'center',
     '&:after': {
       content: '""',
@@ -50,8 +49,6 @@ export const styles = theme => ({
   },
   /* Styles applied to the `Paper` component. */
   paper: {
-    display: 'flex',
-    flexDirection: 'column',
     margin: 48,
     position: 'relative',
     overflowY: 'auto', // Fix IE 11 issue, to remove at some point.
@@ -62,30 +59,25 @@ export const styles = theme => ({
   },
   /* Styles applied to the `Paper` component if `scroll="paper"`. */
   paperScrollPaper: {
-    flex: '0 1 auto',
+    display: 'flex',
+    flexDirection: 'column',
     maxHeight: 'calc(100% - 96px)',
   },
   /* Styles applied to the `Paper` component if `scroll="body"`. */
   paperScrollBody: {
-    margin: '48px auto',
     display: 'inline-block',
     verticalAlign: 'middle',
     textAlign: 'left', // 'initial' doesn't work on IE 11
   },
   /* Styles applied to the `Paper` component if `maxWidth=false`. */
   paperWidthFalse: {
-    '&$paperScrollBody': {
-      margin: 48,
-      maxWidth: 'calc(100% - 96px)',
-    },
+    maxWidth: 'calc(100% - 96px)',
   },
   /* Styles applied to the `Paper` component if `maxWidth="xs"`. */
   paperWidthXs: {
     maxWidth: Math.max(theme.breakpoints.values.xs, 444),
     '&$paperScrollBody': {
-      maxWidth: `calc(${Math.max(theme.breakpoints.values.xs, 444)}px - 96px)`,
       [theme.breakpoints.down(Math.max(theme.breakpoints.values.xs, 444) + 48 * 2)]: {
-        margin: 48,
         maxWidth: 'calc(100% - 96px)',
       },
     },
@@ -94,9 +86,7 @@ export const styles = theme => ({
   paperWidthSm: {
     maxWidth: theme.breakpoints.values.sm,
     '&$paperScrollBody': {
-      maxWidth: `calc(${theme.breakpoints.values.sm}px - 96px)`,
       [theme.breakpoints.down(theme.breakpoints.values.sm + 48 * 2)]: {
-        margin: 48,
         maxWidth: 'calc(100% - 96px)',
       },
     },
@@ -105,9 +95,7 @@ export const styles = theme => ({
   paperWidthMd: {
     maxWidth: theme.breakpoints.values.md,
     '&$paperScrollBody': {
-      maxWidth: `calc(${theme.breakpoints.values.md}px - 96px)`,
       [theme.breakpoints.down(theme.breakpoints.values.md + 48 * 2)]: {
-        margin: 48,
         maxWidth: 'calc(100% - 96px)',
       },
     },
@@ -116,9 +104,7 @@ export const styles = theme => ({
   paperWidthLg: {
     maxWidth: theme.breakpoints.values.lg,
     '&$paperScrollBody': {
-      maxWidth: `calc(${theme.breakpoints.values.lg}px - 96px)`,
       [theme.breakpoints.down(theme.breakpoints.values.lg + 48 * 2)]: {
-        margin: 48,
         maxWidth: 'calc(100% - 96px)',
       },
     },
@@ -127,16 +113,14 @@ export const styles = theme => ({
   paperWidthXl: {
     maxWidth: theme.breakpoints.values.xl,
     '&$paperScrollBody': {
-      maxWidth: `calc(${theme.breakpoints.values.xl}px - 96px)`,
       [theme.breakpoints.down(theme.breakpoints.values.xl + 48 * 2)]: {
-        margin: 48,
         maxWidth: 'calc(100% - 96px)',
       },
     },
   },
   /* Styles applied to the `Paper` component if `fullWidth={true}`. */
   paperFullWidth: {
-    width: '100%',
+    width: 'calc(100% - 96px)',
   },
   /* Styles applied to the `Paper` component if `fullScreen={true}`. */
   paperFullScreen: {
@@ -148,7 +132,6 @@ export const styles = theme => ({
     borderRadius: 0,
     '&$paperScrollBody': {
       margin: 0,
-      width: '100%',
       maxWidth: '100%',
     },
   },

@DominikSerafin
Copy link
Contributor Author

@oliviertassinari I've re-tested it again in the same browsers and props combinations and seems to be working nicely

should I push the commit with these changes or will you do it?

@oliviertassinari
Copy link
Member

oliviertassinari commented May 28, 2019

@DominikSerafin Nice! A new commit would be perfect. I would like to look at the print problem then, see how the changes impact it.

@oliviertassinari oliviertassinari added the new feature New feature or request label May 28, 2019
Copy link
Member

@oliviertassinari oliviertassinari left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Perfect, I have fixed the print mode regression.
Finger crossed, I hope it won't break people code. I would appreciate more people reviewing the changes.

@oliviertassinari
Copy link
Member

I was suspecting a regression, but it's was a false alarm. I have added the "good for merge" label back. We can merge it tomorrow if nobody notices an issue with the changes.

@oliviertassinari oliviertassinari merged commit 9291574 into mui:master May 31, 2019
@oliviertassinari
Copy link
Member

@DominikSerafin Thanks

@DominikSerafin
Copy link
Contributor Author

@oliviertassinari awesome to see it merged! Thank you also for you help 😃

@blasterpistol
Copy link
Contributor

@DominikSerafin how to turn off this behavior? Our dialogs have a dynamic loaded part of an additional content at the end, so when content is loaded dialogs are jumping. Also it's better for user see dialogs in the top of the screen, and not in the center.

@DominikSerafin
Copy link
Contributor Author

@testarossaaaaa I would argue the centered dialogs are generally better (especially when e.g. user has monitor with big resolution or 'portrait' orientation).

As for your use case I think you could just show a spinner or some placeholder during the content loading that would make the Dialog take the entire viewport height (e.g. you could use CSS vh units for that).

Or you can just overwrite Dialog styles so it's on top - https://material-ui.com/customization/components/.

@oliviertassinari
Copy link
Member

oliviertassinari commented Jun 14, 2019

I agree that centered dialogs are a better default. They are a lot of StackOverflow views on this problem with Bootstrap.

Changing the alignment is easy:

/* scroll="paper" */
.MuiDialog-scrollPaper {
  align-items: flex-start;
}

/* scroll="body" */
.MuiDialog-scrollBody:after {
  vertical-align: top;
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
component: dialog This is the name of the generic UI component, not the React module! new feature New feature or request
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants